package controller

import (
	"encoding/json"
	"fx_swift_schedule/api"
	"fx_swift_schedule/internal/service"
	"fx_swift_schedule/pkg/code"
	"fx_swift_schedule/pkg/internalerrors"
	"fx_swift_schedule/pkg/logger"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"io/ioutil"
	"net/http"
)

func Tp1003CallBack(c *gin.Context) {
	req := &api.Tp1003CallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.Tp1003Req{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = service.NewIfcCallBackSrv().Tp1003CallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func Tp1005CallBack(c *gin.Context) {
	req := &api.Tp1005CallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.Tp1005Req{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = service.NewIfcCallBackSrv().Tp1005CallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func Tp2001CallBack(c *gin.Context) {
	req := &api.Tp2001CallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.Tp2001Req{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	head := &api.Tp2001HeadStr{}
	err = json.Unmarshal([]byte(resObj.Head), &head)
	if err != nil {
		logger.SugarLogger.Errorf("head转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = service.NewIfcCallBackSrv().Tp2001CallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func Tp2007CallBack(c *gin.Context) {
	req := &api.Tp2007CallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.Tp2007Req{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = service.NewIfcCallBackSrv().Tp2007CallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func Tp2009CallBack(c *gin.Context) {
	req := &api.Tp2009CallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.Tp2009Req{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = service.NewIfcCallBackSrv().Tp2009CallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func Tp1012CallBack(c *gin.Context) {
	req := &api.Tp1012CallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.Tp1012Req{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = service.NewIfcCallBackSrv().Tp1012CallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func VAInfoCallBack(c *gin.Context) {
	req := &api.VAInfoCallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.VAInfoReq{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = service.NewIfcCallBackSrv().VAInfoCallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 商户入网业务回调接口
func MerChantCallBack(c *gin.Context) {
	req := &api.MerVACallBackReq{}
	body, err := ioutil.ReadAll(c.Request.Body)

	//解密body
	resStr, err := service.DoDecrypt(body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	//返回结果集解析
	resObj := &api.MerChantReq{}
	err = json.Unmarshal(resStr, &resObj)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err = copier.Copy(&req, &resObj.Body)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}

	err = service.NewIfcCallBackSrv().MerChantCallBack(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}
